package com.shure.modules.docs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shure.common.query.condition.Condition;
import com.shure.modules.docs.entity.Templates;
import com.shure.modules.docs.query.TemplatesQuery;
import com.shure.modules.docs.service.TemplatesService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
public class TemplateController {

    private final TemplatesService templateService;

    public TemplateController(TemplatesService templateService) {
        this.templateService = templateService;
    }

    // 新增模板
    @PostMapping
    public ResponseEntity create(@RequestBody Templates template) {
        boolean b = templateService.save(template);
        return ResponseEntity.ok(template);
    }

    // 查询模板列表
    @GetMapping("list")
    public ResponseEntity<List<Templates>> list(TemplatesQuery query) {
        LambdaQueryWrapper<Templates> wrapper = Condition.getLambdaQueryWrapper(query);
        return ResponseEntity.ok(templateService.list(wrapper));
    }

    // 查询模板
    @GetMapping("/{id}")
    public ResponseEntity<Templates> getById(@PathVariable("id") Long id) {
        Templates template = templateService.getById(id);
        return ResponseEntity.ok(template);
    }

    // 更新模板
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable("id") Long id, @RequestBody Templates template) {
        template.setId(id);
        return ResponseEntity.ok(templateService.updateById(template));
    }

    // 删除模板
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        templateService.removeById(id);
        return ResponseEntity.noContent().build();
    }
}
